uni.$u.http.setConfig((config) => {
	/*  config 为默认全局配置  */
	config.baseURL = `https://xiheapp.damaizf.com:29266/appletserver`; /* 根域名 */
	return config;
});
// 白名单  无 openid 和 token 可进入
let whiteList = ['/applet/appletUser/authorized', '/applet/appletUser/selectAuthorizedUser',
	'/applet/message/inform/appMessageList', '/applet/articles/questionsOrScience',
	'/applet/personal/center/jsonToRSA', '/applet/carousel/list', '/applet/appoint/recordList',
	'/applet/babel/babelList', '/applet/articles/recommendByLabel', '/applet/babel/babelList'
]

// 是否弹窗过
let isShowModal = false


uni.$u.http.interceptors.request.use(
	(config) => {
		// 可使用async await 做异步操作
		let flag = whiteList.includes(config.url)
		if (flag) {
			config.header = {
				...config.header,
			};
			return config;
		} else {
			let deptId = uni.getStorageSync("deptId");
			let openId = uni.getStorageSync("openId");
			if (!openId) {
				if (!flag) {
					// uni.showModal({
					// 	title: '',
					// 	content: '请登录后查看',
					// 	success: function(res) {
					// 		if (res.confirm) {
					// 			uni.navigateTo({
					// 				url: '/pages/Login/index'
					// 			});
					// 		} else if (res.cancel) {
					// 			uni.navigateBack({
					// 				delta: 1
					// 			})
					// 		}
					// 	}
					// });

				}

			}
			config.header = {
				...config.header,
				deptId: deptId,
				openId: openId,
			};
			return config;
		}

	},
	(config) => {
		// 可使用async await 做异步操作
		return Promise.reject(config);
	}
);

uni.$u.http.interceptors.response.use(
	(response) => {
		/* 对响应成功做点什么 可使用async await 做异步操作*/
		console.log(response.data, 'response.data.code');
		if (response.data.code !== 200) {
			// 服务端返回的状态码不等于200，则reject()
			let openId = uni.getStorageSync("openId");
			if (openId) {
				uni.showToast({
					icon: "none",
					title: response.data.message,
					duration: 2000
				});
				setTimeout(() => {
					return Promise.reject(response); // return Promise.reject 可使promise状态进入catch
				}, 2000)
			} else {
				if (!isShowModal) {
					isShowModal = true
					uni.showModal({
						title: '',
						content: '请登录后查看',
						success: function(res) {
							isShowModal = false
							if (res.confirm) {
								uni.redirectTo({
									url: '/pages/Login/index'
								});
							} else if (res.cancel) {
								uni.navigateBack({
									delta: 1
								})
							}
						}
					});
				}

				// uni.showToast({
				// 	icon: "none",
				// 	title: '请进行登录',
				// 	duration: 2000
				// });
				// setTimeout(() => {
				// 	return Promise.reject(response); // return Promise.reject 可使promise状态进入catch
				// }, 2000)
			}

		} else {
			// uni.showToast({
			// 	icon: "none",
			// 	title: response.data.message,
			// 	duration: 2000
			// });
			return response.data;
		}

	},
	(response) => {
		/*  对响应错误做点什么 （statusCode !== 200）*/
		uni.showToast({
			icon: "none",
			title: '数据错误！请联系客服',
			duration: 2000
		});
		setTimeout(() => {
			uni.navigateTo({
				url: '/pages/Login/index'
			});
		}, 2000)
		return Promise.reject(response);
	}
);